package project.active;

import java.util.ArrayList;
import java.util.Date;

import model.pojo.Employee;
import project.model.PrProject;
import project.dao.EmployeeDAO;
import project.dao.ProjectDAO;
import approve.action.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class LProjectdetail extends Activity {
	Bundle language;
	EditText namepro,despro,datestartpro,datecompletepro;
	Spinner managerpro;
	Employee emp;
	RadioButton rdSt0,rdSt1;
	Button btUpdate, btDelete, btMenuProjectList, btInsert;
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	String actionChoose="newpro";
	TextView lbproid,lbproname,lbprodes,lbprods,lbprodc,lbpromana;
	String lang="EN",database;
	TextView logout,username;
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lprojectdetail);
		language = getIntent().getExtras().getBundle(getLang());
		
		Intent ii=this.getIntent();
		actionChoose = (String)ii.getSerializableExtra("proaction");
		emp=(Employee)ii.getSerializableExtra("em");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		if(actionChoose.equals("newpro")){
			loadNewInfo();
		}else if(actionChoose.equals("editpro")){
			loadUpdateInfo();
		}
	}
	
	public void loadUpdateInfo(){
		lbproid=(TextView)findViewById(R.id.l_tvProjectID);
		lbproname=(TextView)findViewById(R.id.l_tvProjectName);
		lbprodes=(TextView)findViewById(R.id.l_tvDescription);
		lbprods=(TextView)findViewById(R.id.l_tvDateStart);
		lbprodc=(TextView)findViewById(R.id.l_tvDateComplete);
		lbpromana=(TextView)findViewById(R.id.l_tvManager);
		
		//idpro=(EditText)findViewById(R.id.l_txtIDProject);
		namepro=(EditText)findViewById(R.id.l_txtProjectName);
		despro=(EditText)findViewById(R.id.l_txtDescription);
		datestartpro=(EditText)findViewById(R.id.l_txtDateStart);
		datecompletepro=(EditText)findViewById(R.id.l_txtDateCompelete);
		managerpro=(Spinner)findViewById(R.id.l_cbManager);
		rdSt0=(RadioButton)findViewById(R.id.l_rdStatus0);
		rdSt1=(RadioButton)findViewById(R.id.l_rdStatus1);
		
		Intent i=this.getIntent();
		pr=(PrProject)i.getSerializableExtra("project");
		
		lbproid.setText(language.getString("ac_MNLProjectCode")+": "+pr.getIdProject());
		lbproname.setText(language.getString("ac_SUProjectName")+"(*)");
		lbprodes.setText(language.getString("Description"));
		lbprods.setText(language.getString("start_date"));
		lbprodc.setText(language.getString("complete_date"));
		lbpromana.setText(language.getString("ac_Manager"));
		rdSt0.setText(language.getString("notcomplete"));
		rdSt1.setText(language.getString("complete"));
		
		ArrayAdapter<Employee> arr = new ArrayAdapter<Employee>(this,android.R.layout.simple_spinner_item,loadManager());
		managerpro.setAdapter(arr);
		
		
		//idpro.setText(pr.getIdProject());
		namepro.setText(pr.getNameProject());
		despro.setText(pr.getDescription());
		datestartpro.setText(pr.getDateStart().toString());
		datecompletepro.setText(pr.getDateComplete().toString());
		

		for(Integer k=0;k<arr.getCount();k++){
			if(arr.getItem(k).getIdemployee().equals(pr.getIdManager())){
				managerpro.setSelection(k);
				break;
			}
		}
		
		
		if(pr.getStatus()==0){
			rdSt0.setChecked(true);
			rdSt1.setChecked(false);
		}else if(pr.getStatus()==1){
			rdSt1.setChecked(true);
			rdSt0.setChecked(false);
		}
		
		btUpdate=(Button)findViewById(R.id.l_btUpInfoPro);
		btDelete=(Button)findViewById(R.id.l_btDeInfoPro);
		btMenuProjectList=(Button)findViewById(R.id.l_btUpInfoMenu);
		
		btDelete.setText(language.getString("Delete"));
		btUpdate.setText(language.getString("ac_CapNhatThongTin"));
		btMenuProjectList.setText(language.getString("project_info"));
		
		btMenuProjectList.setOnClickListener(goProjectListMenu);
		btUpdate.setOnClickListener(updateProjectInfo);
		//btDelete.setOnClickListener(deletProjectInfo);
		
		btDelete.setOnClickListener(showdialog);
		
		rdSt0.setOnClickListener(checkSt0);
		rdSt1.setOnClickListener(checkSt1);
		
		
	}
	
	private OnClickListener showdialog = new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			openDialogDelete();
		}
	};
	
	public void loadNewInfo(){
		//idpro=(EditText)findViewById(R.id.l_txtIDProject);
		namepro=(EditText)findViewById(R.id.l_txtProjectName);
		despro=(EditText)findViewById(R.id.l_txtDescription);
		datestartpro=(EditText)findViewById(R.id.l_txtDateStart);
		datecompletepro=(EditText)findViewById(R.id.l_txtDateCompelete);
		managerpro=(Spinner)findViewById(R.id.l_cbManager);
		rdSt0=(RadioButton)findViewById(R.id.l_rdStatus0);
		rdSt1=(RadioButton)findViewById(R.id.l_rdStatus1);
		
		ArrayAdapter<Employee> arr = new ArrayAdapter<Employee>(this,android.R.layout.simple_spinner_item,loadManager());
		managerpro.setAdapter(arr);
		
		rdSt0.setChecked(true);
		rdSt1.setChecked(false);
		
		Date dd=new Date();
		pr.setIdProject(""+dd.getTime());
		//pr.setDateStart(dd);
		pr.setStatus(0);
//		idpro.setText(pr.getIdProject());
		//datestartpro.setText(pr.getDateStart().toString());
		
		btMenuProjectList=(Button)findViewById(R.id.l_btUpInfoMenu);
	//	btInsert=(Button)findViewById(R.id.l_btInsertProInfo);
		btMenuProjectList.setOnClickListener(goProjectListMenu);
	//	btInsert.setOnClickListener(insertProjectInfo);
				
		
		rdSt0.setOnClickListener(checkSt0);
		rdSt1.setOnClickListener(checkSt1);
	}
	
	private OnClickListener checkSt0 =new OnClickListener() {
		
//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(rdSt0.isChecked()){
				rdSt1.setChecked(false);
				pr.setStatus(0);
			}else{
				rdSt1.setChecked(true);
				pr.setStatus(1);
			}
		}
	};
	
	private OnClickListener checkSt1 = new OnClickListener() {
		
//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(rdSt1.isChecked()){
				rdSt0.setChecked(false);
				pr.setStatus(1);
			}else{
				rdSt0.setChecked(true);
				pr.setStatus(0);
			}
		}
	};
	
	public ArrayList<Employee> loadManager(){
		ArrayList<Employee> mn=new ArrayList<Employee>();
		EmployeeDAO emdao=new EmployeeDAO();
		mn=emdao.getAllEmployee(getDatabase());
		return mn;
	}
	
//	private OnClickListener insertProjectInfo = new OnClickListener() {
//		
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			if(!checkNull()){
//				pr.setNameProject(namepro.getText().toString());
//				pr.setDescription(despro.getText().toString());						
//				pr.setIdManager(emdao.getOneEmployeeByName(managerpro.getSelectedItem().toString(),getDatabase()).getIdemployee());
//				
//				pr.setDateStart((Date) datestartpro.getText());
////				pr.setDateComplete((Date) datecompletepro.getText());
//				
//				if(!prodao.checkExitInsert(pr, getDatabase())){
////					if(prodao.insertProjectInfo(pr, getDatabase())>0){
////						System.out.println("them thanh cong");
////					}else{
////						System.out.println("khong them dc");
////					}
//					System.out.println("check ok");
//				}else{
//					System.out.println("da co");
//				}
//			}
//		}
//	};
	
	private OnClickListener goProjectListMenu = new OnClickListener() {
		
//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			goback();
		}
	};
	
	public void goback(){
//		Intent i= new Intent(LProjectdetail.this,Projectlist.class);
//		i.putExtra("lang", getLang());
//		i.putExtra("database", getDatabase());
//		i.putExtra("em",emp);
//		startActivity(i);
		Intent i= new Intent(LProjectdetail.this,LSubprojectlistmenu.class);
		i.putExtras(getIntent().getExtras());
		i.putExtra("lang", getLang());
		i.putExtra("database", getDatabase());
		i.putExtra("em",emp);
		i.putExtra("project", pr);
		
		startActivity(i);
	}
	
	public void gobackMM(){
//		Intent i= new Intent(LProjectdetail.this,Projectlist.class);
//		i.putExtra("lang", getLang());
//		i.putExtra("database", getDatabase());
//		i.putExtra("em",emp);
//		startActivity(i);
		Intent i= new Intent(LProjectdetail.this,Projectlist.class);
		i.putExtras(getIntent().getExtras());
		i.putExtra("lang", getLang());
		i.putExtra("database", getDatabase());
		i.putExtra("em",emp);
		i.putExtra("project", pr);
		
		startActivity(i);
	}
	
	private OnClickListener updateProjectInfo = new OnClickListener() {
		
//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(!checkNull()){
				pr.setNameProject(namepro.getText().toString());
				pr.setDescription(despro.getText().toString());						
				pr.setIdManager(emdao.getOneEmployeeByName(managerpro.getSelectedItem().toString(),getDatabase()).getIdemployee());
				
				String ds=datestartpro.getText().toString()+" 00:00:00";
				String dc=datecompletepro.getText().toString()+" 00:00:00";
				//System.out.println("dc: "+dc);
				pr.setdS(ds);
				pr.setdC(dc);
				
				try {
					if(!prodao.checkExitUpdate(pr, getDatabase())){
						if(prodao.updateProjectInfo(pr,getDatabase())>0){
							goback();
						}else{
							openDialogCNKhongDuoc();
						}
					}else{
						openDialogCNExit();
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				openDialogNull();
			}
		}
	};
	
	private OnClickListener deletProjectInfo = new OnClickListener() {
		
//		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.out.println("id: "+pr.getIdProject());
			try {
				if(prodao.deletProjectAllInfo(pr,getDatabase())>0)
					gobackMM();
				else
					openDialogXoaKhongDuoc();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	public void deletepro(){
		try {
			if(prodao.deletProjectAllInfo(pr,getDatabase())>0)
				gobackMM();
			else
				openDialogXoaKhongDuoc();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean checkNull(){
		boolean kq=false;
		if( namepro.getText().toString().equals(null) || datestartpro.getText().toString().equals(null) || datecompletepro.getText().toString().equals(null) ||  namepro.getText().toString().equals("") || datestartpro.getText().toString().equals("") || datecompletepro.getText().toString().equals("")){
			kq=true;
		}		
		return kq;
	}
	
	private void openDialogNull() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("ac_tb_DataNotBlank"));
		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private void openDialogCNKhongDuoc() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃƒÂªu Ã„â€˜Ã¡Â»ï¿½
		builder.setMessage(language.getString("ac_UpdateFail"));
		builder.setCancelable(false);// Set cÃƒÂ³ cho ngÃ†Â°Ã¡Â»ï¿½i dÃƒÂ¹ng Cancer bÃ¡ÂºÂ±ng nÃƒÂºt
										// quay lÃ¡ÂºÂ¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃƒÂ m cÃƒÂ¡i gÃƒÂ¬ Ã„â€˜ÃƒÂ³ khi Ã¡ÂºÂ¥n Yes tÃ¡ÂºÂ¡i Ã„â€˜ÃƒÂ¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private void openDialogCNExit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃƒÂªu Ã„â€˜Ã¡Â»ï¿½
		builder.setMessage(language.getString("ac_tb_CSDLDaTonTai"));
		builder.setCancelable(false);// Set cÃƒÂ³ cho ngÃ†Â°Ã¡Â»ï¿½i dÃƒÂ¹ng Cancer bÃ¡ÂºÂ±ng nÃƒÂºt
										// quay lÃ¡ÂºÂ¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃƒÂ m cÃƒÂ¡i gÃƒÂ¬ Ã„â€˜ÃƒÂ³ khi Ã¡ÂºÂ¥n Yes tÃ¡ÂºÂ¡i Ã„â€˜ÃƒÂ¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private void openDialogXoaKhongDuoc() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃƒÂªu Ã„â€˜Ã¡Â»ï¿½
		builder.setMessage(language.getString("ac_DeleteFail"));
		builder.setCancelable(false);// Set cÃƒÂ³ cho ngÃ†Â°Ã¡Â»ï¿½i dÃƒÂ¹ng Cancer bÃ¡ÂºÂ±ng nÃƒÂºt
										// quay lÃ¡ÂºÂ¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃƒÂ m cÃƒÂ¡i gÃƒÂ¬ Ã„â€˜ÃƒÂ³ khi Ã¡ÂºÂ¥n Yes tÃ¡ÂºÂ¡i Ã„â€˜ÃƒÂ¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private void openDialogDelete() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃƒÂªu Ã„â€˜Ã¡Â»ï¿½
		builder.setMessage(language.getString("ac_tb_Delete"));
		builder.setCancelable(true);// Set cÃƒÂ³ cho ngÃ†Â°Ã¡Â»ï¿½i dÃƒÂ¹ng Cancer bÃ¡ÂºÂ±ng nÃƒÂºt
										// quay lÃ¡ÂºÂ¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃƒÂ m cÃƒÂ¡i gÃƒÂ¬ Ã„â€˜ÃƒÂ³ khi Ã¡ÂºÂ¥n Yes tÃ¡ÂºÂ¡i Ã„â€˜ÃƒÂ¢y
				
				deletepro();
				dialog.dismiss();
				
			}
		});
		
		builder.setNegativeButton(language.getString("cancel"), null);

		builder.show();
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(LProjectdetail.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
